import os
import django



from django.db import models
from django.contrib.auth.models import AbstractUser
from django.contrib.auth.models import User
class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    is_leader = models.BooleanField('是否为领导', default=False)

    class Meta:
        db_table = 'app_userprofile'  # 指定表名

        verbose_name = '用户信息'
        verbose_name_plural = '用户信息'
class Colleges(models.Model):
    id = models.AutoField('记录编号', primary_key=True)
    name = models.CharField('学院名称',  max_length=32, null=False)
    createTime = models.CharField('建立时间', db_column='create_time', max_length=19)
    class Meta:
        db_table = 'colleges'




class DictType(models.Model):
    id = models.AutoField('字典编号', primary_key=True)
    type_code = models.CharField('字典类型编码', max_length=64, null=False)
    type_name = models.CharField('字典名称', max_length=64, null=False)

    class Meta:
        db_table = 'Dict_Type'
        verbose_name = "字典名单"
        verbose_name_plural = "字典列表"

class DictClass(models.Model):
    id = models.AutoField('字典编号', primary_key=True)
    type_code = models.CharField('字典科目编码', max_length=64, null=False)

    class Meta:
        db_table = 'dict_class'


class ShopWhiteList(models.Model):
    id = models.AutoField('白名单编号', primary_key=True)
    # shop_type = models.CharField('店铺类型', max_length=64, null=False)
    shop_type_choice = (
        ('ebay', 'ebay.com店铺'),
        ('ali', 'aliexpress.com店铺'),
    )
    shop_type = models.CharField('店铺类别',
                                 max_length=20,
                                 choices=shop_type_choice)
    shop_name = models.CharField('店铺名称', max_length=64, null=False)
    class Meta:
        db_table = 'shopwhite_list'
        verbose_name = "店铺白名单"
        verbose_name_plural = "店铺白名单列表"
class Fpe(models.Model):
    id = models.AutoField('跟踪编号', primary_key=True)
    class Meta:
        db_table = 'fpe_nothing'
        verbose_name = '对方链接跟价->增删改查'
        verbose_name_plural = '链接跟价列表->增删改查'


class FollowPrice(models.Model):
    id = models.AutoField('跟踪编号', primary_key=True)
    shop_type_choice = (
        ('ebay', 'ebay.com店铺'),
        ('ali', 'aliexpress.com店铺'),
    )
    shop_type = models.CharField('店铺类别',
                                 max_length=20,
                                 choices=shop_type_choice)
    storename = models.CharField('店铺', max_length=64, null=False)
    username = models.CharField('负责人', max_length=64, null=False)
    keywords = models.CharField('关键词', max_length=800, null=False)

    title = models.CharField('标题', max_length=800, null=False)
    title2 = models.CharField('标题2', max_length=800, null=True)
    link = models.CharField('链接', max_length=800, null=False)
    pic = models.CharField('图片', max_length=800, null=True)
    price = models.CharField('美元价格', max_length=64, null=False)
    currency = models.CharField('网站显示货币', max_length=64, null=True)
    salesvolume = models.FloatField('销量', max_length=64, null=True)

    salesvolume2 = models.FloatField('销量2', max_length=64, null=True)
    pic2 = models.CharField('图片2', max_length=800, null=True)
    price2 = models.CharField('价格2', max_length=64, null=True)
    itemid = models.CharField('我方产品ID', max_length=800, null=False)
    lowvalue = models.CharField('跟价最低价格', max_length=64, null=False)
    opdate = models.DateTimeField('操作日期', auto_now=False, auto_now_add=True)
    followstatus = models.CharField('跟价状态',
                                    max_length=64,
                                    default='未跟价',
                                    null=True)

    @property
    def safe_itemid(self):
        return self.itemid or ''
    class Meta:
        db_table = 'follow_price'
        verbose_name = '对方链接跟价-'
        verbose_name_plural = '对方链接跟价列表'
        permissions = [
            ("can_manage_followprice_admin",
             "Can manage followprice in admin"),
        ]

class SkuKeywords(models.Model):
    username = models.CharField('创建人', max_length=64, null=True)

    id = models.AutoField('关键词编号', primary_key=True)
    sku = models.CharField('sku编码', max_length=64, null=True)
    keywords = models.CharField('关键词', max_length=64, null=False)
    chargeman = models.CharField('负责人', max_length=64, null=True)

    lowvalue = models.FloatField('跟价最低金额', null=False)
    ebay = models.BooleanField('ebay.com店铺', null=False, default=False)
    ali = models.BooleanField('aliexpress.com店铺', null=False, default=False)
    followstatus = models.CharField('是否跟价过竞品',
                                    max_length=64,
                                    default='未跟价',
                                    null=True)
    captured = models.CharField('抓取状态',default='未抓取', max_length=64, null=True)
    opdate = models.DateTimeField('更新时间', auto_now=True)
    def save(self, *args, **kwargs):
        if not self.username and hasattr(self, '_current_user'):
            self.username = self._current_user.username
        super().save(*args, **kwargs)
    class Meta:
        db_table = 'sku_keywords'
        verbose_name = "关键词sku名单"
        verbose_name_plural = "关键词sku列表"


class SkuKeywords_history(models.Model):
    historyid = models.AutoField('关键词编号', primary_key=True)

    id = models.IntegerField('关键词编号', default=0)
    sku = models.CharField('item id', max_length=64, null=True)
    keywords = models.CharField('关键词', max_length=64, null=False)
    chargeman = models.CharField('负责人', max_length=64, null=True)
    opdate = models.DateTimeField('更新时间', auto_now=True)
    username = models.CharField('创建人', max_length=64, null=True)

    lowvalue = models.FloatField('跟价最低金额', null=True)
    fromprice = models.FloatField('从价格', null=True)
    toprice = models.FloatField('改到价格', null=True)

    ebay = models.BooleanField('ebay.com店铺', null=False, default=True)
    ali = models.BooleanField('aliexpress.com店铺', null=False, default=True)
    followstatus = models.CharField('是否跟价过竞品',
                                    max_length=64,
                                    default='未跟价',
                                    null=False)
    captured = models.CharField('抓取状态',
                                default='未抓取',
                                max_length=64,
                                null=True)

    class Meta:
        db_table = 'sku_keywords_history'
        verbose_name = "关键词下载-SKU跟价记录"
        verbose_name_plural = "关键词下载-SKU跟价记录"

class SkuKeywordsAdminViewProxy(FollowPrice):
    class Meta:
        proxy = True
        db_table = 'sku_keywords'
        verbose_name = "关键词sku名单"
        verbose_name_plural = "关键词sku列表"


class SkuKeywordsAnotherProxy(FollowPrice):

    class Meta:
        proxy = True
        verbose_name = "竞品链接跟价"


class Rates(models.Model):
    id = models.AutoField('汇率编号', primary_key=True)
    currency_name = models.CharField('货币名称', max_length=64, null=True)
    currency_simplename = models.CharField('货币简称-需要和网站一致',
                                           max_length=64,
                                           null=True)
    rate = models.FloatField('汇率', max_length=64, null=True)
    opdate = models.DateTimeField('更新时间', auto_now=True)

    class Meta:
        db_table = 'rates'
        verbose_name = "货币汇率列"

        verbose_name_plural = '当前主流货币汇率列表'


class CrawlExcept(models.Model):
    id = models.AutoField('异常编号', primary_key=True)
    shop_type_choice = (
        ('ebay', 'ebay.com店铺'),
        ('ali', 'aliexpress.com店铺'),
    )
    opdate = models.DateTimeField('操作日期', auto_now=False, auto_now_add=False, null=True)

    shop_type = models.CharField('关键词类别',
                                 max_length=20,
                                 choices=shop_type_choice)
    keywords = models.CharField('关键词', max_length=64, null=True)
    exceptcon = models.CharField('异常信息 ', max_length=2000, null=True)

    class Meta:
        db_table = 'CrawlExcept'
        verbose_name = "下载异常"


class FollowPriceHistory(models.Model):
    id = models.AutoField('跟踪编号', primary_key=True)
    shop_type_choice = (
        ('ebay', 'ebay.com店铺'),
        ('ali', 'aliexpress.com店铺'),
    )
    shop_type = models.CharField('店铺类别',
                                 max_length=20,
                                 choices=shop_type_choice)
    storename = models.CharField('店铺', max_length=64, null=False)
    username = models.CharField('负责人', max_length=64, null=False)
    keywords = models.CharField('关键词', max_length=64, null=False)
    title = models.CharField('标题', max_length=64, null=False)
    link = models.CharField('链接', max_length=64, null=False)
    pic = models.CharField('图片', max_length=64, null=True)
    price = models.CharField('美元价格', max_length=64, null=False)
    currency = models.CharField('网站显示货币', max_length=64, null=True)

    salesvolume = models.FloatField('销量', max_length=64, null=True)

    salesvolume2 = models.FloatField('销量2', max_length=64, null=True)
    pic2 = models.CharField('图片2', max_length=64, null=True)
    price2 = models.CharField('价格2', max_length=64, null=True)
    title2 = models.CharField('标题2', max_length=64, null=True)

    itemid = models.CharField('我方产品ID', max_length=64, null=False)
    lowvalue = models.CharField('跟价最低价格', max_length=64, null=False)
    opdate = models.DateTimeField('操作日期', auto_now=False, auto_now_add=True)
    username = models.CharField('操作人员', max_length=64, null=True)
    followstatus = models.CharField('是否被跟价',
                                    default='未跟价',
                                    max_length=64,
                                    null=False)

    class Meta:
        db_table = 'follow_price_history'
        verbose_name = '对方链接跟价历史'
        verbose_name_plural = '对方链接跟价历史列表'
